package cn.sirun.com.fca.map.view.impl;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.cloud.iov.flow.view.IContentViewContainer;
import com.tencent.cloud.iov.router.Router;
import com.tencent.cloud.iov.util.ActivityUtils;
import com.tencent.lbssearch.TencentSearch;
import com.tencent.lbssearch.httpresponse.BaseObject;
import com.tencent.lbssearch.httpresponse.HttpResponseListener;
import com.tencent.lbssearch.object.Location;
import com.tencent.lbssearch.object.param.SearchParam;
import com.tencent.lbssearch.object.param.SuggestionParam;
import com.tencent.lbssearch.object.result.SearchResultObject;
import com.tencent.lbssearch.object.result.SuggestionResultObject;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import cn.sirun.com.fca.map.R;
import cn.sirun.com.fca.map.adapter.PoiResultAdapter;
import cn.sirun.com.fca.map.adapter.SearchResultAdapter;
import cn.sirun.com.fca.map.constant.MapActivityConst;
import cn.sirun.com.fca.map.listener.MyItemLongClickListener;
import cn.sirun.com.fca.map.model.SearchResultData;
import cn.sirun.com.fca.map.model.bean.AddressBean;
import cn.sirun.com.fca.map.presenter.IMapPresenter;
import cn.sirun.com.fca.map.uitl.AddressHistoryUtils;
import cn.sirun.com.fca.map.view.impl.MapBaseLoadingFlowView;
import cn.sirun.com.fca.map.widget.MapView;

import static com.tencent.cloud.iov.util.ResourcesUtils.getResources;

public class MapMenuFlowView extends MapBaseLoadingFlowView{

    private MapView mMapView;
    private RecyclerView lvdata;
    private SearchResultAdapter searchResultAdapter;
    private PoiResultAdapter poiResultAdapter;
    private TextView tv_name;
    private String flag;
    private ImageView iv_goback;
    private LinearLayout llmenuSearch;
    private String din;

    public MapMenuFlowView(Activity activity) {
        super(activity);
    }

    public MapMenuFlowView(Activity activity, IContentViewContainer container) {
        super(activity, container);
    }


    @Override
    public void onCreate() {
        super.onCreate();
        View view = LayoutInflater.from(getContext())
                .inflate(R.layout.map_menu_result_fragment, null, false);
        setContentView(view);
        mMapView = view.findViewById(R.id.map);
        tv_name = view.findViewById(R.id.tv_map_menu_name);
        iv_goback = view.findViewById(R.id.iv_map_menu_go_back);
        llmenuSearch = view.findViewById(R.id.ll_menu_search);
        llmenuSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
        lvdata = view.findViewById(R.id.rlv_map_menu_result);
        lvdata.setLayoutManager(new LinearLayoutManager(getContext(),
                LinearLayoutManager.VERTICAL, false));
        Intent intent = getActivity().getIntent();
        // 获取传递的值
        flag = intent.getStringExtra("menu");
        if(flag.equals("one")){
            tv_name.setText(getResources().getString(R.string.map_menu_gasstation));
            searchPoi();
//            suggestion(tv_name.getText().toString());
        }else if(flag.equals("two")){
            tv_name.setText(getResources().getString(R.string.map_menu_charge));
            searchPoi();
//            suggestion(tv_name.getText().toString());
        }else if(flag.equals("three")){
            tv_name.setText(getResources().getString(R.string.map_menu_cate));
            searchPoi();
//            suggestion(tv_name.getText().toString());
        }else if(flag.equals("four")){
            tv_name.setText(getResources().getString(R.string.map_menu_hotel));
            searchPoi();
//            suggestion(tv_name.getText().toString());
        }else {
            tv_name.setText(flag);
            searchPoi();
        }
    }

    /**
     * poi检索
     */
    protected void searchPoi() {
        TencentSearch tencentSearch = new TencentSearch(getContext());
        String keyWord = tv_name.getText().toString().trim();
        //城市搜索
        SearchParam.Region region = new SearchParam.Region().poi("北京").//设置搜索城市
                autoExtend(false);//设置搜索范围不扩大
        //圆形范围搜索
        Location location1 = new Location().lat(39.984154f).lng(116.307490f);
        SearchParam.Nearby nearBy = new SearchParam.Nearby().point(location1).r(1000);
        //矩形搜索，这里的范围是故宫
        Location location2 = new Location().lat(39.913127f).lng(116.392164f);
        Location location3 = new Location().lat(39.923034f).lng(116.402078f);
        SearchParam.Rectangle rectangle = new SearchParam.Rectangle().point(location2, location3);

        //filter()方法可以设置过滤类别，
        //search接口还提供了排序方式、返回条目数、返回页码具体用法见文档，
        //同时也可以参考官网的webservice对应接口的说明
        SearchParam searchParam = new SearchParam().keyword(keyWord).boundary(region);
        tencentSearch.search(searchParam, new HttpResponseListener() {

            @Override
            public void onFailure(int arg0, String arg1, Throwable arg2) {
                // TODO Auto-generated method stub
//                printResult(arg1);
            }

            @Override
            public void onSuccess(int arg0, BaseObject arg1) {
                // TODO Auto-generated method stub
                if (arg1 == null) {
                    return;
                }
                SearchResultObject obj = (SearchResultObject) arg1;
                if(obj.data == null){
                    lvdata.setVisibility(View.GONE);
                    return;
                }
                if(poiResultAdapter ==null){
                    poiResultAdapter = new PoiResultAdapter(obj.data,getContext());
                    lvdata.setAdapter(poiResultAdapter);
                }else {
                    poiResultAdapter.setCarStatusData(obj.data);
                    poiResultAdapter.notifyDataSetChanged();
                }
                lvdata.setVisibility(View.VISIBLE);
                poiResultAdapter.setListener(new MyItemLongClickListener() {
                    @Override
                    public void onItemLongClick(View view, int postion) {
                        SearchResultObject.SearchResultData obj ;
                        obj = poiResultAdapter.getCarStatusData().get(postion);
                        lvdata.setVisibility(View.GONE);
                        Intent intent = Router.fromPath(MapActivityConst.Path.MAP_SEND_CAR);
                        intent.putExtra("set","send");
                        intent.putExtra("addresslat",obj.location.lat+"");
                        intent.putExtra("addresslng",obj.location.lng+"");
                        intent.putExtra("title",obj.title);
                        intent.putExtra("address",obj.address);
                        ActivityUtils.startActivity(getActivity(),intent);
                    }
                });
                String result = "搜索poi\n";
                for(SearchResultObject.SearchResultData data : obj.data){
//                    Log.v("SearchDemo","title:"+data.title + ";" + data.address);
//                    result += data.address+"\n";
                }
//                printResult(result);
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        mMapView.onStart();
        searchPoi();
    }

    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
        mMapView.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }

    @Override
    protected IMapPresenter createPresenter() {
        return null;
    }
}
